package cn.xkai.exercise.a;

import com.google.gson.Gson;

/**
 * @description: 反转字符串
 * 自己的思路：利用临时空间交换前后字符
 *      原地法(一般都是异或): 由于数组数据是ascii值，可直接通过异或交换前后字符
 * 借鉴的思路：同理
 * @author: kaixiang
 * @date: 2022/6/26
 **/
public class Solution12 {
    public void reverseString(char[] s) {
        for (int i = 0; i < s.length / 2; i++) {
            s[i] = (char) (s[i] ^ s[s.length - 1 - i]);
            s[s.length - 1 - i] = (char) (s[i] ^ s[s.length - 1 - i]);
            s[i] = (char) (s[i] ^ s[s.length - 1 - i]);
        }
    }

    public static void main(String[] args) {
        char[] s = {'h', 'e', 'l', 'l', 'o'};
        Solution12 solution12 = new Solution12();
        solution12.reverseString(s);
        System.out.println(new Gson().toJson(s));
    }
}
